package classes.view.component 
{
	import classes.controller.EDataObject;
	import classes.controller.EString;
	import classes.controller.VONullObject;
	
	import com.d4p.custom.imageLoader.ImageLoader;
	import com.d4p.custom.imageLoader.ImageLoaderEvent;
	import com.d4p.util.ExternalFontLoader;
	import com.greensock.TweenMax;
	import com.greensock.easing.Cubic;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class PopupRoomListItem extends Sprite
	{
		public var _over_btn:SimpleButton;
		public var _this_num:int;
		
		private var _this_obj:Object;
		
		private var _img_mc:MovieClip;
		private var _c1_mc:MovieClip;
		private var _c2_mc:MovieClip;
		private var _new_mc:MovieClip;
		private var _best_mc:MovieClip;
		private var _moreBtn_mc:MovieClip;
		private var _bg_mc:MovieClip;
		private var _no_txt:TextField;
		private var _title_txt:TextField;
		private var _cateogry_txt:TextField;
		private var _maker_txt:TextField;
		private var _detail_txt:TextField;
		private var _overImg:Sprite;
		
		public function PopupRoomListItem() 
		{
			super();
			setLayOut();
			initListener();
			defaultSetting();
		}
		
		private function setLayOut():void
		{
			this._img_mc		=	this.getChildByName('img_mc') 		as MovieClip;
			this._c1_mc			=	this.getChildByName('c1_mc') 		as MovieClip;
			this._c2_mc			=	this.getChildByName('c2_mc') 		as MovieClip;
			this._new_mc		=	this.getChildByName('new_mc') 		as MovieClip;
			this._best_mc		=	this.getChildByName('best_mc') 		as MovieClip;
			this._moreBtn_mc	=	this.getChildByName('moreBtn_mc') 	as MovieClip;
			this._bg_mc			=	this.getChildByName('bg_mc') 		as MovieClip;
			
			this._over_btn		=	this.getChildByName('over_btn') 	as SimpleButton;
			
			this._no_txt		=	this.getChildByName('no_txt') 		as TextField;
			this._title_txt		=	this.getChildByName('title_txt') 	as TextField;
			this._cateogry_txt	=	this.getChildByName('cateogry_txt') as TextField;
			this._maker_txt		=	this.getChildByName('maker_txt') 	as TextField;
			this._detail_txt	=	this.getChildByName('detail_txt') 	as TextField;
			this._overImg		= 	this.getChildByName('overImg_mc')	as Sprite;
		}
		
		private function initListener():void
		{
			_over_btn.addEventListener(MouseEvent.CLICK, 		overClick);
			_over_btn.addEventListener(MouseEvent.MOUSE_OVER, 	overOver);
			_over_btn.addEventListener(MouseEvent.MOUSE_OUT, 	overOver);
			
			_moreBtn_mc.addEventListener(MouseEvent.CLICK,		moreView);
		}
		
		private function overOver(e:MouseEvent):void 
		{
			switch (e.type) 
			{
				case "mouseOver":
					TweenMax.to(_overImg, .3, { autoAlpha:1, ease:Cubic.easeOut } );
					break;
				case "mouseOut":
					TweenMax.to(_overImg, .3, { autoAlpha:0, ease:Cubic.easeOut } );
					break;
			}
		}
		
		private function overClick(e:MouseEvent):void 
		{

			dispatchEvent(new EDataObject(_this_obj, EDataObject.EVENT_DATAOBJECT));
			
		}
		
		private function moreView(e:MouseEvent):void 
		{
			dispatchEvent(new EString(_infoData, EString.EVENT_String));
		}
		
		private function defaultSetting():void
		{
			_moreBtn_mc.buttonMode = true;
			this._overImg.alpha = 0;
			this._overImg.visible = false;
		}
		
		
		private var _infoData:String;
		public function setData($num:int, $obj:Object, $total:*):void
		{
			_this_num = $num;
			_this_obj = $obj;
			_this_obj.db_no 		 = $obj.db_no;
			_this_obj.sb_no 		 = $obj.sb_no;
			_this_obj.ch1_no 		 = $obj.ch1_no;
			_this_obj.ch2_no 		 = $obj.ch2_no;
			
			_no_txt.text 			 = $num.toString() + ". ";
			_title_txt.text 		 = VONullObject.getLoadInstance().nullText(_this_obj.name);
			_cateogry_txt.text 		 = VONullObject.getLoadInstance().nullText(_this_obj.subject);
			_maker_txt.text 		 = VONullObject.getLoadInstance().nullText(_this_obj.maker);
			_detail_txt.text 		 = VONullObject.getLoadInstance().nullText(_this_obj.info).substr(0, 20) + '...';
			
			_infoData				 = VONullObject.getLoadInstance().nullText(_this_obj.info);
			
			_title_txt.x 		= _no_txt.x + _no_txt.textWidth + 4;
			_c1_mc.x 			= _title_txt.x + _title_txt.textWidth + 6;
			_cateogry_txt.x 	= _c1_mc.x + 3;
			_c2_mc.x 			= _cateogry_txt.x + _cateogry_txt.textWidth + 6;
			_maker_txt.x 		= _c2_mc.x + 3;
			
			_new_mc.visible = false;
			_best_mc.visible = false;
			if (Number(_this_obj.isNew) == 1 && Number(_this_obj.isBest) == 1) {
				_new_mc.x 		= _maker_txt.x + _maker_txt.textWidth + 10;
				_best_mc.x 		= _new_mc.x + _new_mc.width + 2;
				_new_mc.visible = true;
				_best_mc.visible = true;
			}
			if (Number(_this_obj.isNew) == 1) {
				_new_mc.x 		= _maker_txt.x + _maker_txt.textWidth + 10;
				_new_mc.visible = true;
				
			}
			if (Number(_this_obj.isBest) == 1) {
				_best_mc.x = _maker_txt.x + _maker_txt.textWidth + 10;
				_best_mc.visible = true;
			}
			
			
			if (_this_num % 2 !== 0) {
				_bg_mc.visible = true;
			} else {
				_bg_mc.visible = false;
			}

			imagesLoad = _this_obj.img_icon;
			
		}
		
		private function set imagesLoad($url:String):void
		{
			if($url!=""){
				//imageload.
				var imgLoader:ImageLoader = new ImageLoader();
				imgLoader.addEventListener(ImageLoaderEvent.IMAGE_COMPLETE,	onCompleteImageLoad);	
				try{
		
					imgLoader.load($url, false);
				}
				catch(e:IOErrorEvent){
					
					trace("이미지가 없습니다.");
				}
			}
		}
		private function onCompleteImageLoad(e:ImageLoaderEvent):void
		{
			e.bitmap.width 		= this._img_mc.width; 
			e.bitmap.height 	= this._img_mc.height;
			this._img_mc.addChild(e.bitmap);
			trace('imageload complete');
		}
	}
}